package com.ruoyi.business.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.business.domain.TOrderInfo;
import com.ruoyi.business.domain.TRecipientInfo;
import com.ruoyi.business.response.ResponseData;
import com.ruoyi.business.service.BasePayment;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.HttpUtils;
import com.ruoyi.common.utils.MD5Util;
import com.ruoyi.common.utils.MoneyUtil;
import com.ruoyi.common.utils.SignUtil;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.HashMap;

@Service("aliyinlianPayService")
public class AliPayYinLianServiceImpl extends BasePayment {

    @Override
    public JSONObject createPayOrder(TOrderInfo orderInfo) throws Exception {
        JSONObject jsonObject = new JSONObject();
        String payGateway =sysConfigService.selectConfigByKey("sys.bussine.payGateway");
        TRecipientInfo userRecipientInfo = userRecipientService.selectTRecipientInfoById(orderInfo.getRecipientId());
        HashMap<String, String> headerMap = new HashMap();
        HashMap<String, String> dataMap = new HashMap();
//        String qrcode = userRecipientInfo.getReceiptQrcode();
//        String qrcode_ = HttpUtils.getParam(qrcode,"userAppId");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        //{"busChannelId":"5f30f4666eefc9576f5d7a98","longitude":119.3318,"country":"中国","city":"福州","discountPay":0,"latitude":26.101879,"jhxwsh_id":467804,"money":0}
        String cookieStr = userRecipientInfo.getCookie();
        JSONObject cookieObj = JSONObject.parseObject(cookieStr);
        String url = "https://yspay.zbwlkj.net/scene/unionpay/native";
        JSONObject accountJson = JSONObject.parseObject(userRecipientInfo.getAccountJson());
        dataMap.put("merchantId",userRecipientInfo.getChannelAccount());
        dataMap.put("goodName", MoneyUtil.fenToYuan(orderInfo.getMoney()+"")+"元，获得箱子*1");
        dataMap.put("totalFee",orderInfo.getMoney()+"");
        dataMap.put("attach", MoneyUtil.fenToYuan(orderInfo.getMoney()+"")+"元，获得箱子*1");

        dataMap.put("orderNo",orderInfo.getId()+"");
        dataMap.put("callbackUrl",payGateway+"/notify/aliylpay/"+orderInfo.getId());
        StringBuilder buf = new StringBuilder("");
        SignUtil.buildPayParamsObj(buf, dataMap);
        String preStr = buf.toString();
        String sign = MD5Util.stringMD5(SignUtil.formatUrlMap(dataMap, false) + "5IN7Quys&key=" + accountJson.getString("ylKey"));
        logger.info("参数排序==>待验签数据:" + SignUtil.formatUrlMap(dataMap, false) + "5IN7Quys&key=" + accountJson.getString("ylKey")+"");

        dataMap.put("sign",sign);
        logger.info("银联请求："+JSONObject.toJSONString(dataMap));
        StringBuilder newbuf = new StringBuilder((dataMap.size() + 1) * 10);
        SignUtil.buildPayParamsObj(newbuf, dataMap);
        String newStr = newbuf.toString();
        String result = HttpUtils.sendPostRequest(url,newStr);
        JSONObject jhxwshIdResObj = JSONObject.parseObject(result);
        logger.info("银联返回："+jhxwshIdResObj.toJSONString());

        if(!jhxwshIdResObj.getBoolean("status")){
            jsonObject.put("code","100");
            jsonObject.put("msg",jhxwshIdResObj.getString("msg"));
            return jsonObject;
        }
        JSONObject jhxwshIdData = jhxwshIdResObj.getJSONObject("data");
        String code_url = jhxwshIdData.getString("code_url");
        jsonObject.put("payUrl",code_url);
        jsonObject.put("code","200");
        return jsonObject;
    }

    public JSONObject createWxPayOrder(TOrderInfo orderInfo) throws ServiceException, UnsupportedEncodingException {
        return null;
    }

    @Override
    public ResponseData smartReissueOrder(TOrderInfo orderInfo) throws Exception {
        return new ResponseData();
    }

}
